package com.css.fxfzypg.modules.similitudeAnalyse.controller;


import com.css.fxfzypg.base.response.RestResponse;
import com.css.fxfzypg.modules.similitudeAnalyse.entity.RealEarthquakeEntity;
import com.css.fxfzypg.modules.similitudeAnalyse.param.QueParams;
import com.css.fxfzypg.modules.similitudeAnalyse.param.QueParams1;
import com.css.fxfzypg.modules.similitudeAnalyse.service.SimAnalyseService;
import com.css.fxfzypg.modules.similitudeAnalyse.utils.Xml2ObjectUtil;
import com.css.fxfzypg.util.PlatformPageUtils;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;
import java.util.Map;


/**
 * @author y1fan
 * @create 2022-08-31 14:27
 */
@RestController
@RequestMapping("/similitudeAnalyse")
@Slf4j
public class SimAnalyseController {

    /**
     * 获取数据地址
     */
    private static final String URL = "http://1.eqweixin.sinaapp.com/zxml.php";

    @Resource
    private SimAnalyseService simAnalyseService;


    /**
     * 分析结果保存
     * @return
     * @throws Exception
     */
    @PostMapping("/analyseResultSave")
    public RestResponse similitudeAnalyse()throws Exception {
        //调取接口 返回真实数据对象
        List<RealEarthquakeEntity> realEarthquakeEntities = Xml2ObjectUtil.getRealEarthquakeEntity(URL);
        if (realEarthquakeEntities == null || realEarthquakeEntities.size()==0) {
            return RestResponse.fail("数据为空");
        }
        simAnalyseService.similitudeAnalyse(realEarthquakeEntities);
        return RestResponse.succeed("分析并保存成功");
    }

    /***
     * 分页查询
     * @param queParams
     * @return
     */
    @GetMapping("/page")
    public RestResponse getPage(QueParams1 queParams, HttpServletRequest request){
        RestResponse response = null;
        try{
            int curPage = Integer.valueOf(request.getParameter(PlatformPageUtils.CUR_PAGE));
            int pageSize=Integer.valueOf(request.getParameter(PlatformPageUtils.PAGE_SIZE));
            Map<String,Object> page=simAnalyseService.getPage(queParams,curPage,pageSize);
            response = RestResponse.succeed(page);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }

    /***
     * 查看
     * @param id
     * @return
     */
    @GetMapping("/fingById")
    public RestResponse fingById(@RequestParam("id") String id){
        RestResponse response = null;
        try{
            Map<String,Object> map=simAnalyseService.fingById(id);
            response = RestResponse.succeed(map);
        }catch (Exception e){
            String errorMessage = "查询失败!";
            log.error(errorMessage,e);
            response = RestResponse.fail(errorMessage);
        }
        return response;
    }
}
